package boun.center;

import java.io.IOException;
import java.text.ParseException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class CMPENewsActivity extends Activity {
	ListView lv;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cmpe_list);
		lv = (ListView) findViewById(R.id.list_cmpe);
		new FetchingTask().execute();
	}
	class FetchingTask extends AsyncTask<Void,Void,CMPENewsAdapter>{
		ProgressDialog dialog;
		public FetchingTask(){
			dialog = new ProgressDialog(CMPENewsActivity.this);
		}
		@Override
		protected CMPENewsAdapter doInBackground(Void... params) {
			CMPENewsAdapter ca;
			try {
				ca = new CMPENewsAdapter(CMPENewsActivity.this);
				return ca;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPreExecute(){
			dialog = ProgressDialog.show(getParent(),"","Fetching..",true);
		}
		@Override
		protected void onPostExecute(CMPENewsAdapter la){
			dialog.dismiss();
			lv.setAdapter(la);
			lv.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,
						long id) {
					CmpeNews news = (CmpeNews) parent.getItemAtPosition(position);
					AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
					//builder.setTitle(news.getTitle());
					TextView TitleView = new TextView(getParent());
					TitleView.setText(news.getSubject());
					TitleView.setBackgroundColor(Color.DKGRAY);
					TitleView.setTextSize(20);
					TitleView.setPadding(10, 10, 10, 10);
					TitleView.setGravity(Gravity.CENTER);
					builder.setCustomTitle(TitleView);
					builder.setMessage(news.getContent());
					builder.setNegativeButton("Close", new OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog, int arg1) {
							dialog.dismiss();
						}});
					AlertDialog dialog = builder.create();
					dialog.show();
				}
			});
		}
	}
}